<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>发布订阅</title>
</head>

<body>
  <script>
    class Dep {
      constructor() {
        this.subs = []
      }
      addSub(watch) {
        this.subs.push(watch)
      }
      notify() {
        this.subs.forEach((item) => {
          item.update()
        })
      }
    }

    class Watch {
      constructor(cb) {
        this.cb = cb
      }
      update() {
        this.cb()
      }
    }
    const w1 = new Watch(() => { console.log("w1回调"); })
    const w2 = new Watch(() => { console.log("w2回调"); })
    const dep = new Dep()
    dep.addSub(w1)
    dep.addSub(w2)

    dep.notify();


  </script>
</body>

</html>